package com.zhengbing.tj.service.impl;

import com.zhengbing.tj.entity.AuditLog;
import com.zhengbing.tj.service.AsyncAuditLogService;
import com.zhengbing.tj.service.AuditLogService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 异步审计日志服务实现类
 * 使用@Async注解实现异步记录审计日志
 *
 * @author zhengbing
 * @date 2024-08-22
 */
@Service
public class AsyncAuditLogServiceImpl implements AsyncAuditLogService {
    
    private static final Logger logger = LoggerFactory.getLogger(AsyncAuditLogServiceImpl.class);
    
    @Autowired
    private AuditLogService auditLogService;
    
    @Override
    @Async("auditLogExecutor")
    public void recordAuditLogAsync(AuditLog log) {
        try {
            if (log != null) {
                auditLogService.recordAuditLog(log);
            }
        } catch (Exception e) {
            // 记录异常但不影响主业务流程
            logger.error("Failed to record audit log asynchronously: {}", e.getMessage(), e);
        }
    }
    
    @Override
    @Async("auditLogExecutor")
    public void batchRecordAuditLogsAsync(List<AuditLog> logs) {
        try {
            if (logs != null && !logs.isEmpty()) {
                auditLogService.batchRecordAuditLogs(logs);
            }
        } catch (Exception e) {
            // 记录异常但不影响主业务流程
            logger.error("Failed to record batch audit logs asynchronously: {}", e.getMessage(), e);
        }
    }
}